package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TypeMapper;
import com.bwie.pojo.Brand;
import com.bwie.pojo.Type;
import com.bwie.service.BrandService;
import com.bwie.mapper.BrandMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 陶仕豪
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-05-23 16:28:42
*/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
    implements BrandService{

    @Autowired
    BrandMapper brandMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result getBrandList(PageInfoVo pageInfoVo) {
        Page<Brand> brandPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
       wrapper.lambda().orderByDesc(Brand::getCreateTime);


       if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
           wrapper.lambda().like(Brand::getBrandName,pageInfoVo.getKeyWord());
       }
       Page<Brand> page= brandMapper.selectPage(brandPage,wrapper);
        return Result.ok().setData(page);
    }

    @Override
    public Result addBrand(Brand brand) {
        String token = request.getHeader("token");
        if(token==null){
            return Result.error().setMsg("请先登录");
        }
        Brand one = baseMapper.selectOne(new QueryWrapper<Brand>().lambda().eq(Brand::getBrandName, brand.getBrandName()));
        if(one!=null){
            return Result.error().setMsg("品牌已存在");
        }

        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        brand.setCreateTime(new Date());
        brand.setCreateBy(nickName);


        brandMapper.insert(brand);
        return Result.ok();
    }

    @Override
    public Result UpdateBrandStatus(Brand tbBrand) {
        String token = request.getHeader("token");
        if(token==null){
            return Result.error().setMsg("请先登录");
        }
        Brand brand = brandMapper.selectById(tbBrand.getBrandId());
        if(brand==null){
            return Result.error().setMsg("品牌不存在");
        }
        //修改状态
        brand.setBrandStatus(tbBrand.getBrandStatus());
        //设计审计字段

        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        brand.setUpdateTime(new Date());
        brand.setUpdateBy(nickName);
        brandMapper.updateById(brand);
        return Result.ok();
    }

    @Override
    public Result getBrandListAll() {
        List<Brand> brandList = brandMapper.selectList(null);
        return Result.ok().setData(brandList);
    }


}




